/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.lastproject;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author CANG
 */
public class TreeTableClickListener extends MouseAdapter
{
    /*public void mousePressed(MouseEvent e)
    {
        if(e.isPopupTrigger())
        {
            doPop(e);
        }
    }*/        
    private JTable _jtListDatatable;
    private ServerForm _server;
    //private JTree _jTreeTable;
    
    public TreeTableClickListener(JTable jTable, ServerForm server)
    {
        _jtListDatatable = jTable;
        _server = server;
        //_jTreeTable = treeTable;
    }
    
    private boolean _bTableClick = false;    
    
    public void mouseReleased(MouseEvent e)
    {      
        JTree jClick = (JTree)e.getSource();
        TreePath rightClickPath = jClick.getPathForLocation(e.getX(), e.getY()); 
        if(rightClickPath != null)
        {                        
            jClick.setSelectionPath(rightClickPath);
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)rightClickPath.getLastPathComponent();
            
            TreeNode parent_Node = treeNode.getParent();
            String name = null;
            if(parent_Node != null)
            {
                name = parent_Node.toString();
                _bTableClick = true;
            }
                                    
            if(e.isPopupTrigger())
            {
                doPop(e, jClick);
            }
        }
    }
    
    public void doPop(MouseEvent e, JTree jTreeTable)
    {
        try
        {
            if(_bTableClick)
            {
                JTree jClick = (JTree)e.getSource();
                MyPopupMenu_NodeTable menu_Table = new MyPopupMenu_NodeTable(_jtListDatatable, _server, jClick);
                menu_Table.show(e.getComponent(), e.getX(), e.getY());
                _bTableClick = false;
            }
            else
            {
                MyPopupMenu_Root menu = new MyPopupMenu_Root(_server, jTreeTable);
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
        catch(Exception ex)
        {
            int a = 4;
            a--;
        }
    }
}
